package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.product;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CPdtClass;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CProductsInfo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUserPlantConn;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICPdtClassService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICProductsInfoService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ISysUserPlantConnService;
import com.xiaoxu.intranetweb.util.SzResult;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class PdtClassBu {
    @Autowired
    private ICPdtClassService iCPdtClassService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private ISysUserPlantConnService userPlantConnService;
    @Autowired
    private ICProductsInfoService productsInfoService;


    //    所有品种类型
    public SzResult getProductClass(String plantId) {
        QueryWrapper<CPdtClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PDTCLASS_DELETED", 0);
        queryWrapper.eq("DATA_FROM", plantId);
        List<CPdtClass> cProductsInfoList = iCPdtClassService.list(queryWrapper);
        return SzResult.success(cProductsInfoList);
    }

    //新增品种类型
    public SzResult addProductClass(String pdtClass,String remark, String srvTmsTamp, String cliTmsTamp, String token,String plantId) throws Exception {
      String uuid= java.util.UUID.randomUUID().toString().replace("-", "");
        CPdtClass cPdtClass = new CPdtClass();
        QueryWrapper<CPdtClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PDTCLASS_CLASS", pdtClass);
        queryWrapper.eq("PDTCLASS_DELETED", 0);
        CPdtClass one = iCPdtClassService.getOne(queryWrapper);
        if (one != null) {
            return SzResult.error("该品种类型已存在");
        }
        cPdtClass.setPdtclassClass(pdtClass);
        cPdtClass.setPdtclassRemark(remark);

        QueryWrapper<CPdtClass> orderDetailQueryWrapper = new QueryWrapper<>();
        orderDetailQueryWrapper.orderByDesc("PDTCLASS_ID");
        orderDetailQueryWrapper.last("limit 1");
        orderDetailQueryWrapper.eq("DATA_FROM", plantId);
        CPdtClass pdtClass2 = iCPdtClassService.getOne(orderDetailQueryWrapper);
        if (pdtClass2 != null) {
            BigDecimal pdtclassId = pdtClass2.getPdtclassId();
            String pdtclassId1 = String.valueOf((pdtclassId.intValue() + 1));
            cPdtClass.setPdtclassId(new BigDecimal(pdtclassId1));
            cPdtClass.setId(String.valueOf(new BigDecimal(pdtclassId1)));
        } else {
            cPdtClass.setPdtclassId(new BigDecimal(1));
            cPdtClass.setId(String.valueOf(new BigDecimal(1)));
        }
            cPdtClass.setId(uuid);

        cPdtClass.setPdtclassDeleted(0);
        cPdtClass.setDataFrom(plantId);
        if (srvTmsTamp != null) {
            cPdtClass.setSvrTmstamp(LocalDateTime.parse(srvTmsTamp));
        }
        if (cliTmsTamp != null) {
            cPdtClass.setCliTmstamp(LocalDateTime.parse(cliTmsTamp));
        }
        cPdtClass.setPdtclassNew(0);
        cPdtClass.setPdtclassFromCtrl(0);
        cPdtClass.setDownedID1(0);
        iCPdtClassService.save(cPdtClass);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        sysLogBu.logRecord("新增品种类型", user.getTrueName(), "新增了品种类型为" + pdtClass + "的品种类型",plantId);
        return SzResult.Success();
    }

    //修改品种类型
    public SzResult updateProductClass(String id, String pdtClass,String remark, String token,String plantId) throws Exception {
        QueryWrapper<CPdtClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PDTCLASS_ID", id);
        queryWrapper.eq("PDTCLASS_DELETED", 0);
        CPdtClass one = iCPdtClassService.getOne(queryWrapper);
        if (one == null) {
            return SzResult.error("该品种类型不存在");
        }
        if (pdtClass != null) {
            //pdtClass不能是特殊字符
            if (pdtClass.matches(".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"].*")) {
                return SzResult.error("品种类型不能包含特殊字符");
            }
            QueryWrapper<CPdtClass> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("PDTCLASS_CLASS", pdtClass);
            queryWrapper1.eq("PDTCLASS_DELETED", 0);
            queryWrapper1.ne("PDTCLASS_ID", id);
            CPdtClass one1 = iCPdtClassService.getOne(queryWrapper1);
            if (one1 != null) {
                return SzResult.error("该品种类型已存在,请重新输入");
            }
            one.setPdtclassClass(pdtClass);
        }
        if (remark != null) {
            one.setPdtclassRemark(remark);
        }
        one.setDownedID1(0);
        iCPdtClassService.updateById(one);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        sysLogBu.logRecord("修改品种类型", user.getTrueName(), "修改了品种类型为" + pdtClass + "的品种类型",plantId);
        return SzResult.Success();
    }

    //删除品种类型
    public SzResult deleteProductClass(String id, String token,String plantId) throws Exception {
        //String plantId = plantId;
        QueryWrapper<CPdtClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("PDTCLASS_DELETED", 0);
        CPdtClass one = iCPdtClassService.getOne(queryWrapper);
        if (one == null) {
            return SzResult.error("该品种类型不存在或已被删除");
        }
        String pdtclassClass = one.getPdtclassClass();
        String string = one.getPdtclassId().toString();
        QueryWrapper<CProductsInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("PDTINF_CLASS", string);
        queryWrapper1.eq("PDTINF_DELETED", 0);
        queryWrapper1.eq("DATA_FROM", plantId);
        queryWrapper1.last("limit 1");
        CProductsInfo one1 = productsInfoService.getOne(queryWrapper1);
        if (one1 != null) {
            return SzResult.error("该品种类型下存在品种,不可删除");
        }
        one.setPdtclassDeleted(1);
        one.setDownedID1(0);
        iCPdtClassService.updateById(one);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        sysLogBu.logRecord("删除品种类型", user.getTrueName(), "删除了品种类型为" + pdtclassClass + "的品种类型",plantId);
        return SzResult.Success();
    }

    //品种来源控制
    public SzResult productClassfromCtrl(String id, String isOrNot, String token,String plantId) throws Exception {
        QueryWrapper<CPdtClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", id);
        queryWrapper.eq("PDTCLASS_DELETED", 0);
        CPdtClass one = iCPdtClassService.getOne(queryWrapper);
        if (one == null) {
            return SzResult.error("该品种类型不存在或已被删除");
        }
        if (isOrNot.equals("-1")) {
            one.setPdtclassFromCtrl(-1);
            one.setDownedID1(0);
            iCPdtClassService.updateById(one);
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            sysLogBu.logRecord("品种来源控制", user.getTrueName(), "控制了品种类型为" + one.getPdtclassClass() + "的品种来源",plantId);
            return SzResult.success("品种来源已控制");
        } else {
            one.setPdtclassFromCtrl(0);
            one.setDownedID1(0);
            iCPdtClassService.updateById(one);
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            sysLogBu.logRecord("品种来源控制", user.getTrueName(), "取消了品种类型为" + one.getPdtclassClass() + "的品种来源控制",plantId);
            return SzResult.success("品种来源未控制");
        }
    }
}
